package aynu.hd.Hotel.Controller;

import aynu.hd.Hotel.Service.roomService;
import aynu.hd.Hotel.Service.userService;
import aynu.hd.pojo.Room;
import aynu.hd.pojo.Schedule;
import aynu.hd.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("room")
public class roomController {
    @Autowired
    roomService roomService;
    @Autowired
    userService userService;

    //进入房间详情页面
    @GetMapping("schedule_details")
    public String schedule_details(String room_id, Model model, HttpSession session) {
        User user = (User) session.getAttribute("user");
        //是否收藏
        String collect = roomService.queryCollectRoomByUser(user, room_id);
        //房间信息
        Room room = roomService.queryRoomById(room_id);
        //查看房间评价
        List<Schedule> scheduleList = roomService.queryRoomComment(room_id);
        //收藏最多的房间
        List<Room> rooms = roomService.queryRoomCollectMany();
        model.addAttribute("room", room);
        model.addAttribute("collect", collect);
        model.addAttribute("scheduleList", scheduleList);
        model.addAttribute("rooms", rooms);
        return "hotel/room/schedule_details";
    }

    //收藏房间
    @GetMapping("roomCollect")
    @ResponseBody
    public String roomCollect(String id, HttpSession session, Model model) {
        String data = userService.roomCollect(id, session);
        model.addAttribute("collect", "TRUE");
        return data;
    }

    //取消收藏房间
    @GetMapping("roomCollectOver")
    @ResponseBody
    public String roomCollectOver(String id, HttpSession session, Model model) {
        String data = userService.roomCollectOver(id, session);
        model.addAttribute("collect", "FALSE");
        return data;
    }

    //预定房间
    @GetMapping("roomReserve")
    @ResponseBody
    public String roomReserve(String id, HttpSession session) {
        String data = userService.roomReserve(id, session);
        return data;
    }

    //取消预定房间
    @GetMapping("removeSchedule")
    @ResponseBody
    public String removeSchedule(String schedule_id) {
        String data = userService.removeSchedule(schedule_id);
        return data;
    }

    //付款界面
    @GetMapping("addRoomOrder")
    public String addRoomOrder(String schedule_id, Model model) {
        Schedule schedule = userService.addRoomOrder(schedule_id);
        model.addAttribute("schedule", schedule);
        return "hotel/reserve/schedule_order";
    }

    //评论页面
    @RequestMapping("modifyScheduleComment")
    public String modifyScheduleComment(Schedule schedule, Model model) {
        Schedule schedule1 = roomService.queryRoomCommentById(schedule);
        model.addAttribute("schedule",schedule1);
        return "hotel/room/room_comment";
    }

    //评论
    @PostMapping("addComment")
    @ResponseBody
    public String addComment(Schedule schedule){
        roomService.addComment(schedule);
        return "OK";
    }

}
